package com.sfh.dog.ui;

import java.io.File;

import android.view.View;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

import com.sfh.dog.entity.Dog;
import com.sfh.dog.entity.User;
import com.sfh.dog.util.NetWorkHelper;
import com.sfh.dog.util.StringUtils;

/**
 * 添加宠物信息
 * 
 * @author Swer
 * 
 */
public class AddDogActivity extends ADogActivity
{
    
    public void init()
    {
        dogEntity = new Dog();
        tvTitle.setText("添加宠物");
    }
    

    @Override
    public void saveDog()
    {
        if (!NetWorkHelper.isNetworkAvailable(getApplication()))
        {
            return ;
        }
        if (isNull())
        {
            
            return ;
        }
        
        if (StringUtils.isNullOrEmpty(filePath))
        {
            //  showToast("头像不能为空");
            return ;
        }
        
        final User bmobUser = BmobUser.getCurrentUser(AddDogActivity.this,User.class);
        if (bmobUser != null)
        {
            pBLoad.setVisibility(View.VISIBLE);
            dogEntity.setUserID(bmobUser.getObjectId());
            final BmobFile bmobFile = new BmobFile(new File(filePath));
            bmobFile.upload(this, new UploadFileListener()
            {
                @Override
                public void onSuccess()
                {
                    dogEntity.setImage(bmobFile.getFileUrl());
                    dogEntity.save(AddDogActivity.this, new SaveListener()
                    {
                        
                        @Override
                        public void onSuccess()
                        {
                            bmobUser.setDogID(dogEntity.getObjectId());
                            bmobUser.update(AddDogActivity.this, new UpdateListener()
                            {
                                @Override
                                public void onSuccess()
                                {
                                    pBLoad.setVisibility(View.GONE);
                                    finish();
                                }
                                
                                @Override
                                public void onFailure(int arg0, String arg1)
                                {
                                    pBLoad.setVisibility(View.GONE);
                                }
                            });
                          
                        }
                        
                        @Override
                        public void onFailure(int arg0, String arg1)
                        {
                            pBLoad.setVisibility(View.GONE);
                        }
                    });
                    
                }
                
                @Override
                public void onFailure(int arg0, String arg1)
                {
                    pBLoad.setVisibility(View.GONE);
                }
            });
        }
    }
}
